<?php
/**
 * @name:Modle Class images
 * @date:2010.04.16
 * @Author:雅典娜之子
 */

!defined('ACCESSING') && exit('Forbidden');

class images extends Base {
    var $filename;
    var $filesize;
    var $filetype;
    var $tmp_name;
    var $error;
    var $errormsg;
    var $istype;
    var $prefix;
    var $newname;
    var $dirname;
    var $base;
    var $result;
    var $uploadfile;

    public function  __construct($files,$inputname)
    {
        global $db,$BASE;
        if(is_array($files))
        {
            $this->db = $db;
            $this->base = $BASE;
            $this->dirname = date("Ymd",time());
            $this->filename = $files[$inputname]['name'];
            $this->filesize = $files[$inputname]['size'];
            $this->filetype = $files[$inputname]['type'];
            $this->tmp_name = $files[$inputname]['tmp_name'];
            $this->error = $files[$inputname]['error'];
            $this->errormsg = null;
            $this->istype = array();
            $this->prefix = array();
            $this->newname = array();
            $this->result = array();
            $this->uploadfile = array();
        }
    }

    public function images($files,$inputname)
    {
        $this->__construct($files,$inputname);
    }

    public function upload()
    {
        if(is_array($this->filetype))
        {
            foreach($this->filetype as $key=>$value)
            {
                switch ($value)
                {
                     case 'image/pjpeg' :
                         $istype[$key] = 1;
                         $prefix[$key] = ".jpg";
                         break;
                     case 'image/jpeg' :
                         $istype[$key] = 1;
                         $prefix[$key] = ".jpg";
                         break;
                     case 'image/gif' :
                         $istype[$key] = 1;
                         $prefix[$key] = ".gif";
                         break;
                     case 'image/png' :
                         $istype[$key] = 1;
                         $prefix[$key] = ".png";
                         break;
                 }

                 if($this->error[$key] == 0 && !empty($istype[$key]))
                 {
                     $newname[$key] = md5($this->tmp_name[$key]);
                     $dir = D_R."/uploads/".$this->dirname;

                     if(!is_dir($dir))
                     {
                         mkdir($dir);
                     }

                     $this->uploadfile[$key]['tname'] = $newname[$key].$prefix[$key];
                     $this->uploadfile[$key]['tpath'] = "/uploads/".$this->dirname."/";
                     $this->uploadfile[$key]['dateline'] = time();
					 $this->result[] = $this->uploadfile[$key];
                     move_uploaded_file($this->tmp_name[$key],$dir."/".$newname[$key].$prefix[$key]);
                 }
            }
			return true;
        }
    }

    public function InsertData()
    {
        $post = parent::getPost();
        if(!empty($post['category']))
        {
            if(is_array($this->result))
            {
                foreach($this->result as $key=>$value)
                {
					//echo "1";
                    $sqlconn = "INSERT INTO `jm_images`(`tname`,`tpath`,`dateline`,`attribute`) VALUES('".$value['tname']."','".$value['tpath']."',".$value['dateline'].",".$post['category'].")";
                    $this->db->query($sqlconn);
                }
				return true;
            }
            else
            {
                $sqlconn = "INSERT INTO `jm_images`(`tname`,`tpath`,`dateline`,`attribute`) VALUES('".$this->result['tname']."','".$this->result['tpath']."',".$this->result['dateline'].",".$post['category'].")";
                $rs = $this->db->query($sqlconn);
                if(!$rs) {
                    return false;
                }
                else {
                    return true;
                }
            }
        }
    }
}
?>
